<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function httpRequest($api, $postData = array())
{
    //1.初始化
    $ch = curl_init();
    //2.配置
    //2.1设置请求地址
    curl_setopt($ch, CURLOPT_URL, $api);
    //2.2数据流不直接输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //2.3POST请求
    if ($postData) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    }
    //curl注意事项，如果发送的请求是https，必须要禁止服务器端校检SSL证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //3.发送请求
    $data = curl_exec($ch);
    //4.释放资源
    curl_close($ch);
    return json_decode($data, true);
}
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return json_decode($response, true);
    return array($httpCode, $response);
}
function GetfourStr($len)
{
  $chars_array = array(
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
    "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
    "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
    "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
    "S", "T", "U", "V", "W", "X", "Y", "Z",
  );
  $charsLen = count($chars_array) - 1;
  
  $outputstr = "";
  for ($i=0; $i<$len; $i++)
  {
    $outputstr .= $chars_array[mt_rand(0, $charsLen)];
  }
  return $outputstr;
}
function GetfourNumber($len)
{
  $chars_array = array(
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
  );
  $charsLen = count($chars_array) - 1;
  
  $outputstr = "";
  for ($i=0; $i<$len; $i++)
  {
    $outputstr .= $chars_array[mt_rand(0, $charsLen)];
  }
  return $outputstr;
}
function makeSign($param)
{
    //签名步骤一：按字典序排序数组参数
    ksort($param);
    $param = array_filter($param);
    $string = toUrlParams($param);
    //签名步骤三：MD5加密
    $strings = md5($string);
    //签名步骤四：所有字符转为大写
    $result = strtoupper($strings);

    return $result;
}

//参数拼接 url: key=value&key=value
function toUrlParams($param)
{
    $string = '';
    if ($param) {
        $array = [];
        foreach ($param as $key => $val) {
            $array[] = $key . '=' . $val;
        }
        $string = implode("&", $array);
    }

    return $string;
}
